package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ClientBean;
import com.project.dao.IClientDao;
import com.project.service.IClientService;
import com.project.util.InfoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lyl
 * description:
 */
@Service
@Transactional
public class ClientServiceImpl implements IClientService {
    @Autowired
    private IClientDao iClientDao;

    @Override
    public Page<ClientBean> showClientByPage(int pageNum, String clientName, String clientScope) {
        QueryWrapper<ClientBean> wa = new QueryWrapper<>();
        if (!"1".equals(clientName)) {
            wa.like("clientName", clientName);
        }
        if (!"1".equals(clientScope)) {
            wa.like("clientScope", clientScope);
        }
        return iClientDao.selectPage(new Page<>(pageNum, 3), wa);
    }

    @Override
    public ClientBean findClientById(int clientId) {
        return iClientDao.selectById(clientId);
    }

    @Override
    public InfoResult addClient(ClientBean clientBean) {
        int x = iClientDao.insert(clientBean);
        if (x == 1) {
            return new InfoResult(0, "添加" + clientBean.getClientName() + "成功");
        }
        return new InfoResult(1, "添加" + clientBean.getClientName() + "失败");
    }

    @Override
    public InfoResult updateClientById(int clientId, String clientPeo, String clientTel, String clientScope) {
        ClientBean clientBean = iClientDao.selectById(clientId);
        clientBean.setClientPeo(clientPeo);
        clientBean.setClientTel(clientTel);
        clientBean.setClientScope(clientScope);
        int x = iClientDao.updateById(clientBean);
        if (x == 1) {
            return new InfoResult(0, "客户" + clientBean.getClientName() + "更新成功");
        }
        return new InfoResult(1, "客户" + clientBean.getClientName() + "更新失败");
    }

    @Override
    public List<ClientBean> showAll() {
        return iClientDao.selectList(null);
    }
}
